package com.timguan.render.dao.manager.cms;

import com.timguan.render.dao.persistence.cms.ext.CmsDataDOMapperExt;
import com.timguan.render.dataapi.api.ICmsDataManager;
import com.timguan.render.dataapi.domain.CmsDataDO;
import com.timguan.render.dataapi.domain.CmsDataDOExample;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created by guankaiqiang on 2015/3/20.
 */
public class CmsDataManagerImpl implements ICmsDataManager {
    @Resource
    private CmsDataDOMapperExt cmsDataDOMapperExt;

    /**
     * 修改记录
     *
     * @param record
     * @return
     */
    public boolean update(CmsDataDO record) {
        if (record == null) {
            return false;
        }
        //乐观锁
        CmsDataDOExample cmsDataDOExample = new CmsDataDOExample();
        cmsDataDOExample.createCriteria().andVersionEqualTo(record.getVersion()).andIdEqualTo(record.getId());
        record.setVersion(record.getVersion() + 1);
        record.setGmtModified(new Date());
        record.setGmtCreate(null);
        return cmsDataDOMapperExt.updateByExampleSelective(record, cmsDataDOExample) != 0;
    }

    /**
     * 根据条件查询
     *
     * @param listId
     * @return
     */
    public List<CmsDataDO> selectByListId(int listId) {
        if (listId <= 0) {
            return null;
        }
        CmsDataDOExample cmsDataDOExample = new CmsDataDOExample();
        cmsDataDOExample.createCriteria().andIsDeleteEqualTo(0).andListIdEqualTo(listId);
        cmsDataDOExample.setOrderByClause("weight desc,id asc");
        return cmsDataDOMapperExt.selectByExampleWithBLOBs(cmsDataDOExample);
    }

    /**
     * 获取最小权重值
     *
     * @param listId
     * @return
     */
    public int minWeight(int listId) {
        if (listId <= 0) {
            return 0;
        }
        CmsDataDOExample cmsDataDOExample = new CmsDataDOExample();
        cmsDataDOExample.createCriteria().andListIdEqualTo(listId).andIsDeleteEqualTo(0);
        Integer minWeight = cmsDataDOMapperExt.minWeight(cmsDataDOExample);
        return minWeight == null ? -1 : minWeight;//当前无记录返回-1
//        Integer minWeight = cmsDataDOMapperExt.minWeight(condition);
//        return minWeight == null ? -1 : minWeight;//当前无记录返回-1
    }
}
